package io.renren.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.QuestionOptionInfo;
import io.renren.dao.ExamDatailsDao;
import io.renren.entity.ExamDatailsEntity;
import io.renren.entity.ExamTemplateEntity;
import io.renren.util.QuestionUtil;
import io.renren.service.ExamDatailsService;
import io.renren.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 考试题目详情
 * @author huolikang
 */
@Service("examDatailsService")
public class ExamDatailsServiceImpl extends ServiceImpl<ExamDatailsDao, ExamDatailsEntity> implements ExamDatailsService {
    @Autowired
    private ExamService examService;


    /***
     * 插入详情表(把题目放到exam_detail中)
     * @param studentId 学员id
     * @param examId 试卷id
     * @param examPaper 试卷模板
     */
    @Override
    public void createExam(Long studentId, Integer examId, ExamTemplateEntity examPaper) {
        // 单选题数量
        int radioNumber = examPaper.getRadioNumber() == null ? 0 : examPaper.getRadioNumber();
        // 多选题数量
        int checkNumber = examPaper.getCheckNumber() == null ? 0 : examPaper.getCheckNumber();
        // 判断题数量
        int judegNumber = examPaper.getJudegNumber() == null ? 0 : examPaper.getJudegNumber();
        Integer subjectId = examPaper.getSubjectId();
        //插入单选题
        baseMapper.insertRadioNumber(examId, studentId, subjectId, radioNumber);
        //插入多选题
        baseMapper.insertCheckNumber(examId, studentId, subjectId, radioNumber, checkNumber);
        //插入判断题
        baseMapper.insertJudegNumber(examId, studentId, subjectId, radioNumber, checkNumber, judegNumber);
    }

    /***
     * 查看题目的状态(是否提交)
     * @param studentId 学员
     * @param sort 序号
     * @param examId 试卷id
     * @return R
     */
    @Override
    public List<Map<String, Object>> getIsQuestion(Long studentId, Integer sort, Integer examId) {
        //获取题目
        List<Map<String, Object>> getQuestion = baseMapper.getQuestion(studentId, sort, examId);
        return getQuestion;
    }

    /***
     * 根据试卷题目 一题一题获取题目
     * @param studentId 学员id
     * @param sort 序号
     * @param examId 试卷id
     * @return R
     */
    @Override
    public Map<String, Object> getQuestionOneByOne(Long studentId, Integer sort, Integer examId) {

        //获取题目
        Map<String, Object> getQuestionOneByOne = baseMapper.getQuestionOneByOne(studentId, sort, examId);
        if (getQuestionOneByOne != null && getQuestionOneByOne.size()>0) {
            // 1表示收藏，0表示没收藏
            Integer isCollection = getQuestionOneByOne.get("isCollection") == null ? 0
                    : Double.valueOf(
                    getQuestionOneByOne.get("isCollection")
                            .toString()).intValue();
            getQuestionOneByOne.put("isCollection", isCollection);
        }
        //获取考试信息  主要获取剩余时间
        Map<String, Object> paperMap = examService.getPaperMap(examId);
        if (paperMap != null && paperMap.size() > 0) {
            //当前试卷考试剩余时间
            Integer remainExamSecond = paperMap.get("remainExamSecond") == null ? 0
                    : Double.valueOf(paperMap.get("remainExamSecond").toString()).intValue();
            getQuestionOneByOne.put("remainExamSecond", remainExamSecond);
        }
        //拿出题目的选项  让选项变成 A.xxxx  B.xxxx
        String optionContent = getQuestionOneByOne.get("optionContent").toString();
        List<QuestionOptionInfo.OptionInfo> optionInfoList = new ArrayList<QuestionOptionInfo.OptionInfo>();
        QuestionOptionInfo questionBean = new QuestionOptionInfo();
        String[] strArr = optionContent.split("@@");
        for (int i = 0; i < strArr.length; i++) {
            QuestionOptionInfo.OptionInfo optionInfoBean = questionBean.new OptionInfo();
            optionInfoBean.setOptionContent(strArr[i]);
            optionInfoBean.setOptionLetter(QuestionUtil.getOptionArr()[i]);
            optionInfoList.add(optionInfoBean);
            getQuestionOneByOne.put("optionInfo", optionInfoList);
            getQuestionOneByOne.remove("optionContent");
        }

        return getQuestionOneByOne;
    }
}
